package cn.lijigou.shopping.service;

import java.util.HashMap;
import java.util.Map;

import cn.lijigou.shopping.domain.MLocation;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.LocationManager;

public class LocationService {
	private static Context mContext = null;
	private static boolean isLocation = true;
	public static Map<String, MLocation> locations = new HashMap<String, MLocation>();

	public LocationService() {
		super();
	}
	
	public LocationService(Context context) {
		this.mContext = context;
	}

	public void start() {
//		LocationGps gps = new LocationGps(mContext);
//		gps.start();
		LocationMapBar mapBar = new LocationMapBar(mContext);
		mapBar.start();
	}

	public static void select() {
		MLocation mGps = locations.get(LocationManager.GPS_PROVIDER);
		MLocation mNetwork = locations.get(LocationManager.NETWORK_PROVIDER);
		MLocation mMapbar = locations.get("mapbar");
		if (mGps != null) {
			MSystem.mLocation = mGps;
			save(mGps);
		} else if (mNetwork != null) {
			MSystem.mLocation = mNetwork;
			save(mNetwork);
		} else if (mMapbar != null) {
			MSystem.mLocation = mMapbar;
			save(mMapbar);
		}
	}

	public static void save(final MLocation location) {
		
		SharedPreferences sp = mContext.getSharedPreferences("info",
				Context.MODE_PRIVATE);
		Editor edit = sp.edit();
		edit.putString("LBSlng", String.valueOf(location.getLng()));
		edit.putString("LBSlat", String.valueOf(location.getLat()));
		edit.putString("city", location.getCity());
		edit.putString("address", location.getAddress());
		if(isLocation){
			isLocation = false;
			new Thread(new Runnable() {
				
				public void run() {
					MSystem.location_stat(String.valueOf(location.getLat()), String.valueOf(location.getLng()), location.getCity(), location.getAddress());
				}
			}).start();
		}

	}
}
